MQTT

Message Queue Telemetry Transport

The product mainly uses Message Queue Telemetry Transport (MQTT) protocol to communicate between the sensor board and the mobile application. MQTT was designed to be a lightweight, reliable protocol in situations with limited bandwidth and spotty connections. The crux of the protocol is the message broker, a service that is running all of the time on a server. For W.A.S.T.E., this broker lives on an Amazon Web Services server. By housing the broker on an AWS server, it allows the user to receive updates on the status of their house regardless of what network they are on, even on cellular networks. The broker determines what messages are sent to what users. The way it does this is by the topic system.

A given client can connect to the broker, and subscribe to the topic, e.g. “Front Door,” and receive all messages sent to the topic “Front Door.” Additionally, it can publish to whatever topic it wants. The namesake of the MQTT protocol is message queueing, where if a given client is subscribed to a topic, but unexpectedly drops connection, then the broker will retain all of the messages sent to that topic, and forward them to the client when it reconnects. In the product, MQTT is utilized at quality-of-service 1, which means that every message will be delivered at least once.

Connecting to MQTT From the Sensor Side

The program reads the non-volatile memory to see if there is Wi-Fi and MQTT information stored. If so, then the ESP8266 connects to the Wi-Fi network using the stored information. If not, it sets up an AP server with automatic redirect. The user connects to the network set up by the chip, named “WASTESensor,” and inputs their Wi-Fi and MQTT information. When this is submitted, the chip saves it to specific places in the non-volatile memory, and connects to the specified Wi-Fi network.

Once connected, the program reads the EEPROM to see if the triggered flag is set to true. If it is, it connects as a client to the MQTT server and publishes a message to the topic defined by the user that says it has been triggered. It then resets the triggered flag to zero, or logical false, and saves it to memory. If it has not been triggered, it connects and publishes a message that it is secure.

After it publishes a message, the program gracefully disconnects from both the MQTT server and the Wi-Fi network. The chip then sets a background timer for 30 seconds and goes into deep sleep mode to conserve battery.

More Links to Resources